<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 创建对象发送请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
    <button>点击获取段子</button>
    <button>本地server</button>
    <button>本地server - test</button>

    <script>
        //
        let btns = document.querySelectorAll('button');
        //实例对象
        // 针对请求多个服务的场景
        let remoteAxios = axios.create({
            baseURL: 'http://api.xiaohigh.com',
            timeout: 2000
        });// 会返回一个与 axios 功能相同的『函数对象』

        let localAxios = axios.create({
            baseURL: 'http://127.0.0.1:8090',
            timeout: 2000
        });

        //绑定事件
        btns[0].onclick = async function(){
            let result = await remoteAxios.get('/duanzi')
            console.log(result);
        }

        //第二个按钮
        btns[1].onclick = async function(){
            let result = await localAxios.get('/server')
            console.log(result);
        }

        //第三个按钮
        btns[2].onclick = async function(){
            let result = await localAxios.get('/test')
            console.log(result);
        }
    </script>
</body>
</html>